package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import com.atsmart.app.model.MessageInfo;
import com.atsmart.app.model.MessageListInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

public class MessageSqlHelper {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	public synchronized void addMessageInfo(MessageInfo at) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into message_infos(mes_id,name,content,date,receiveFlag,readFlag,type) values(?,?,?,?,?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindString(1, at.getId()==null?"0":at.getId());
			stm.bindString(2, at.getSender() == null ?"":at.getSender());
			stm.bindString(3, at.getContent() == null ?"":at.getContent());
			stm.bindString(4, at.getDate() == null ?"" :at.getDate());
			stm.bindString(5, at.getReceiveFlag()==null?"":at.getReceiveFlag());
			stm.bindString(6, at.getReadFlag()==null?"":at.getReadFlag());
			stm.bindLong(7, at.getType() == 0 ?0:at.getType());
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			e.printStackTrace();
			// will not happen
		}
	}

	public MessageListInfo getMessageInfo(int begin, int pagesize) {
		String sql = "select _id,mes_id,name,content,date,receiveFlag,readFlag,type from message_infos order by date desc limit "
				+ begin + "," + pagesize;
		Cursor cursor = db.rawQuery(sql, null);
		MessageInfo historie = null;
		List<MessageInfo> nums = null;
		MessageListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<MessageInfo>();
				t = new MessageListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new MessageInfo();
					historie.setId(cursor.getString(1));
					historie.setSender(cursor.getString(2));
					historie.setContent(cursor.getString(3));
					historie.setDate(cursor.getString(4));
					historie.setReceiveFlag(cursor.getString(5));
					historie.setReadFlag(cursor.getString(6));
					historie.setType(cursor.getInt(7));
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setMsgs(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}

	
	public MessageListInfo getMessageInfo() {
		String sql = "select _id,mes_id,name,content,date,receiveFlag,readFlag,type from message_infos";
		Cursor cursor = db.rawQuery(sql, null);
		MessageInfo historie = null;
		List<MessageInfo> nums = null;
		MessageListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<MessageInfo>();
				t = new MessageListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new MessageInfo();
					historie.setId(cursor.getString(1));
					historie.setSender(cursor.getString(2));
					historie.setContent(cursor.getString(3));
					historie.setDate(cursor.getString(4));
					historie.setReceiveFlag(cursor.getString(5));
					historie.setReadFlag(cursor.getString(6));
					historie.setType(cursor.getInt(7));
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setMsgs(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}
	
	public int getCount() {
		String sql = "select _id,mes_id,name,content,date,receiveFlag,readFlag,type from message_infos";
		Cursor cursor = db.rawQuery(sql, null);
		int count = 0;
		try {
			if (cursor != null) {
				count = cursor.getCount();
			}
		} catch (Exception e) {
			return count;
		} finally {
			cursor.close();
		}
		return count;
	}

	public void deleteAttribute() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from message_infos");
	}

}
